package com.sunnada.edu.system.pub.common;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sunnada.edu.system.pub.dao.PubDao;
import com.sunnada.edu.system.pub.util.Utils;


/**
 * explain 日志操作
 * 
 * @author chs 2014-10-20 version 1.0
 */
public class LogInfo {

	private static LogInfo li;

	public static LogInfo getInstance() {
		if (li == null) {
			synchronized (LogInfo.class) {
				if (li == null) {
					li = new LogInfo();
				}
			}
		}
		return li;
	}
	public static void main(String[] args){
	    String info = "[def][a][a]";
	 
	    info = info.replace("[", "");
		 info = info.replace("]", ",");
		 if(info!=null && info.length()>0) {
			 info = info.substring(0, info.length()-1);
		 }
		 String[] infos = info.split(",");
		 for(int i=0;i<infos.length;i++) {
			 System.out.println(infos[i]);
		 }
//	     System.out.println(infos.toString());
	 
	}
	public void info(HttpServletRequest request, Class<?> clazz, String info) {
		String loginUserId = Common.getLoginUserId(request);
		String userAreaCode = Common.getUserAreaCode(request);
		info = "[" + loginUserId + "]" + info;
		Logger log = LoggerFactory.getLogger(clazz);
		log.info(info);
		info = info.replace("[", "");
		 info = info.replace("]", ",");
		 if(info!=null && info.length()>0) {
			 info = info.substring(0, info.length()-1);
		 }
		 String[] infos = info.split(",");
		 if(infos!=null && infos.length>1) {
			 String mode="";
			 String content="";
			 String type="";
			 if(infos.length==2) {
				  mode=infos[1];//模块名称
				
				  type="";//操作类型
				  content="";//操作内容
			 }
			 if(infos.length==3) {
				  mode=infos[1];//模块名称
				  type=infos[2];//操作类型
				  content="";//操作内容
			 }
			 if(infos.length>3) {
				  mode=infos[1];//模块名称
				  type=infos[2];
				  content=infos[3];//模块名称
			 } 
			 content= content.replaceAll("'","''");
			PubDao dao = AppContext.getInstance().getPubDao();
			int cot = dao.insert("insert into s_log(REC_ID,CONTENT,MODE_CODE,OPER_TYPE,REC_PERSON,REC_TIME,REC_STATUS,AREA_CODE)"+ 
	        "values(NMEDIA_LOG.NEXTVAL,'"+content+"','"+mode+"','"+type+"','"+loginUserId+"',sysdate,'0','"+userAreaCode+"')"); 
			if(cot==0) {
				System.out.println("【LogInfo.Class】【info()】插入日志异常。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。");
			}
		 }		 
	}

	public void error(HttpServletRequest request, Class<?> clazz, String error) {
		String loginUserId = Common.getLoginUserId(request);
		error = "[" + loginUserId + "]" + error;
		Logger log = LoggerFactory.getLogger(clazz);
		log.error(error);
	}
	
	

	/**
	 * @param param
	 * 	String loginAreaCode=Utils.isNull(param.get("loginAreaCode"))?"":param.get("loginAreaCode");
	    String loginAreaName=Utils.isNull(param.get("loginAreaName"))?"":param.get("loginAreaName");
	    String userId=Utils.isNull(param.get("loginUserId"))?"":param.get("loginUserId");
	    String userName=Utils.isNull(param.get("loginUserName"))?"":param.get("loginUserName");
	    String deptId=Utils.isNull(param.get("loginDeptId"))?"":param.get("loginDeptId");
	    String deptName=Utils.isNull(param.get("loginDeptName"))?"":param.get("loginDeptName");
	    String action=Utils.isNull(param.get("action"))?"":param.get("action");
	    String result=Utils.isNull(param.get("result"))?"":param.get("result");
	 */
	public void option(HashMap<String, String> param){
	    PubDao dao = AppContext.getInstance().getPubDao();
	    String loginAreaCode=Utils.isNull(param.get("loginAreaCode"))?"":param.get("loginAreaCode");
	    String loginAreaName=Utils.isNull(param.get("loginAreaName"))?"":param.get("loginAreaName");
	    String userId=Utils.isNull(param.get("loginUserId"))?"":param.get("loginUserId");
	    String userName=Utils.isNull(param.get("loginUserName"))?"":param.get("loginUserName");
	    String deptId=Utils.isNull(param.get("loginDeptId"))?"":param.get("loginDeptId");
	    String deptName=Utils.isNull(param.get("loginDeptName"))?"":param.get("loginDeptName");
	    String action=Utils.isNull(param.get("action"))?"":param.get("action");
	    String result=Utils.isNull(param.get("result"))?"":param.get("result");
	    String recId=Common.getSeqId("SEQ_S_LOG");
	    dao.insert("insert into s_syslog(REC_ID,AREA_CODE,AREA_NAME,USER_ID,USER_NAME,DEPT_ID,DEPT_NAME,ACTION,REC_TIME,ACTION_RESULT)"+ 
	            "values('"+recId+"','"+loginAreaCode+"','"+loginAreaName+"','"+userId+"','"+userName+"','"+deptId+"','"+deptName+"','"+action+"',now(),'"+result+"')");  
	}
}
